<?php

class PortalWebServiceDataContent extends PortalWebServiceDataBase{

	public function _cast($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Cast',$id,$lang,$format);
	}
	
	public function _folder($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Folder',$id,$lang,$format);
	}

	public function _programme($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Programme',$id,$lang,$format);
	}

	public function _video($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Video',$id,$lang,$format);
	}

	public function _category($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Category',$id,$lang,$format);
	}

	public function _chapter($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Chapter',$id,$lang,$format);
	}

	public function _episode($id,$lang='zh',$format='json'){
		return $this->getFormatDataById('Episode',$id,$lang,$format);
	}

	public function _liveprogramme($video_id,$lang='zh',$format='json'){
		$video_id = $this->__check_int($video_id,'video_id');
		$lang     = $this->__check_lang($lang);
		$format   = $this->__check_format($format);

		$time  = time();
		$p_id  = $this->_getProgrammeIdByVideoId($video_id);
		if(empty($p_id)) return null;
		$query = "id:$p_id AND online_time:[* TO $time] AND offline_time:[$time TO *]";

		$rs = $this->getDataByQuery('Programme',$query);
		if(empty($rs->docs[0])) return null;

		$data = $this->makeProgrammeData($rs->docs[0],$lang);
		return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	private function getMstatIds($type,$video_ids,$category_ids,$period,$offset,$limit,$sort){
		$mstat_api = new MstatWebServiceClient();
		$mstat_ids = $mstat_api->getVideoStatsByPeriod($type,$video_ids,$category_ids,$period,$offset,$limit,$sort);
		return $mstat_ids;
	}

	public function _programmes($video_ids=null,$categorys='all',$offset=0,$limit=10,array $sort=array('online_time'=>'asc'),$start_date=null,$end_date=null,$lang='zh',$format='json'){
		$offset = $this->__check_int($offset,'offset');
		$limit  = $this->__check_int($limit,'limit');
		$sort   = $this->__check_sort_key('Programme',$sort);
		$lang   = $this->__check_lang($lang);
		$format = $this->__check_format($format);

		$programme_ids = $category_ids = $query = array();

		if(strtolower($categorys) != 'all'){
			$categories = explode(',',$categorys);
			foreach($categories as $path){
				$category_ids[] = $this->_getCategoryIdByPath($path);
			}
		}else{
			$categories = array();
		}

		if(array_key_exists('view',$sort) || array_key_exists('favour',$sort)){
			if(empty($end_date)) $end_date = 5;
			$mstat_ids = $this->getMstatIds(2,$video_ids,implode(',',$category_ids),$end_date,$offset,$limit,$sort);
			$data['total']      = $mstat_ids['total'];
			$data['size']       = count($mstat_ids['records']);
			$data['offset']     = $offset;
			$data['limit']      = $limit;
			$data['sort']       = $sort;
			$data['category']   = $categorys;
			$data['start_date'] = $start_date;
			$data['end_date']   = $end_date;
			$data['data'] = array();
		    $mstat_ids = array_keys($mstat_ids['records']);
			foreach($mstat_ids as $p_id){
				$data['data'][$p_id] = $this->_programme($p_id,$lang);
			}
		}else{
			if($video_ids){
				$video_ids = explode(',',$video_ids);
				foreach($video_ids as $vid){
					$vid = $this->__check_int($vid,'video_ids');
					$programme_ids[] = $this->_getProgrammeIdByVideoId($vid);
				}
				if($programme_ids) $query[] = 'id:('.implode(' ',$programme_ids).')';
			}

			if($category_ids) $query[] = 'primary_category_id:('.implode(' ',$category_ids).')';

			if($start_date){
				$start = $this->__check_datetime($start_date,'start_date');
				$query[] = "online_time:[$start TO *]";
			}
			if($end_date){
				$end = $this->__check_datetime($end_date,'end_date');
				$query[] = "offline_time:[* TO $end]";
			}
			$query_str = $query ? implode(' AND ',$query) : 'id:*';

			$data = $this->getFormatDataByQuery('Programme',$query_str,$offset,$limit,$sort,$lang);
			$data['category']   = $categorys;
			$data['start_date'] = $start_date;
			$data['end_date']   = $end_date;
		}
		return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _liveprogrammes($lang='zh',$categorys='all',$offset=0,$limit=10,array $sort=array('online_time'=>'asc'),$format='json'){
		$lang   = $this->__check_lang($lang);
		$offset = $this->__check_int($offset,'offset');
		$limit  = $this->__check_int($limit,'limit');
		$sort   = $this->__check_sort_key('Programme',$sort);
		$format = $this->__check_format($format);

		$time  = time();
		$query_str = "(online_time:[* TO $time] AND offline_time:[$time TO *])";

		$category_ids = array();
		if(strtolower($categorys) != 'all'){
			$categories = explode(',',$categorys);
			foreach($categories as $path){
				$category_ids[] = $this->_getCategoryIdByPath($path);
			}
		}
		if($category_ids) $query_str .= ' AND (primary_category_id:('.implode(' ',$category_ids).'))';

		$data = $this->getFormatDataByQuery('Programme',$query_str,$offset,$limit,$sort,$lang);
		$data['category']   = $categorys;

		return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _videos($video_ids=null,$categorys='all',$offset=0,$limit=10,array $sort=array('online_time'=>'asc'),$start_date=null,$end_date=null,$lang='zh',$format='json'){
		$offset = $this->__check_int($offset,'offset');
		$limit  = $this->__check_int($limit,'limit');
		$sort   = $this->__check_sort_key('Video',$sort);
		$lang   = $this->__check_lang($lang);
		$format = $this->__check_format($format);

		if(strtolower($categorys) != 'all'){
			$categories = explode(',',$categorys);
		}else{
			$categories = array();
		}

		if(array_key_exists('view',$sort) || array_key_exists('favour',$sort)){
			if(empty($end_date)) $end_date = 5;

			$category_ids = array();
			foreach($categories as $path){
				$category_ids[] = $this->_getCategoryIdByPath($path);
			}

			$mstat_ids = $this->getMstatIds(1,$video_ids,implode(',',$category_ids),$end_date,$offset,$limit,$sort);
			$data['total']      = $mstat_ids['total'];
			$data['size']       = count($mstat_ids['records']);
			$data['offset']     = $offset;
			$data['limit']      = $limit;
			$data['sort']       = $sort;
			$data['category']   = $categorys;
			$data['start_date'] = $start_date;
			$data['end_date']   = $end_date;
			$data['data'] = array();
		    $mstat_ids = array_keys($mstat_ids['records']);
			foreach($mstat_ids as $v_id){
				$data['data'][$v_id] = $this->_video($v_id,$lang);
			}
		}else{
			$programme_ids = $query = array();
			foreach($categories as $path){
				$programme_ids = array_merge($programme_ids,$this->_getProgrammeIdsByCategoryPath($path));
			}
			$programme_ids = array_unique($programme_ids);
			if($programme_ids) $query[] = 'programme_id:('.implode(' ',$programme_ids).')';

			if($video_ids) $query[] = 'id:('.str_replace(',',' ',$video_ids).')';  // like to "video in (1,2,3)"

			if($start_date){
				$start = $this->__check_datetime($start_date,'start_date');
				$query[] = "online_time:[$start TO *]";
			}
			if($end_date){
				$end = $this->__check_datetime($end_date,'end_date');
				$query[] = "offline_time:[* TO $end]";
			}

			$query_str = $query ? implode(' AND ',$query) : 'id:*';

			$data = $this->getFormatDataByQuery('Video',$query_str,$offset,$limit,$sort,$lang);
			$data['category']   = $categorys;
			$data['start_date'] = $start_date;
			$data['end_date']   = $end_date;
		}

		return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _freezoneepisodes($lang='zh',$categorys='all',$offset=0,$limit=10,array $sort=array(),$format='json'){
		$format = $this->__check_format($format);
		$query = 'subscription_free_flag:1';
		$programme_ids = $data = array();
		if(strtolower($categorys) != 'all'){
			$categories = explode(',',$categorys);
			foreach($categories as $path){
				$programme_ids = array_merge($programme_ids,$this->_getProgrammeIdsByCategoryPath($path));
			}
			$programme_ids = array_unique($programme_ids);
		}
		if($programme_ids) $query .= ' AND programme_id:('.implode(' ',$programme_ids).')';

		$data = $this->getFormatDataByQuery('Episode',$query,$offset,$limit,$sort,$lang);
		$data['category']   = $categorys;

		return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _freezoneprogrammes($lang='zh',$categorys='all',$offset=0,$limit=10,array $sort=array(),$format='json'){
		$format = $this->__check_format($format);
		$query = 'subscription_free_flag:1';
		$programme_ids = $data = array();
		if(strtolower($categorys) != 'all'){
			$categories = explode(',',$categorys);
			foreach($categories as $path){
				$programme_ids = array_merge($programme_ids,$this->_getProgrammeIdsByCategoryPath($path));
			}
			$programme_ids = array_unique($programme_ids);
		}
		if($programme_ids) $query .= ' AND id:('.implode(' ',$programme_ids).')';

		$data = $this->getFormatDataByQuery('Programme',$query,$offset,$limit,$sort,$lang);
		$data['category']   = $categorys;

		return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}
}
